/*
书籍类模板
 */

import prompt from '@ohos.prompt';
import {DA_HELPER,URI} from './dataAbilityHelperModel';
import dataAbility from '@ohos.data.dataAbility';
//import {User} from './usermodel';

const TAG:string = 'BookModel'

/* Api测试
{"ret":0,https://api.jike.xyz/situ/book/isbn/9787517097471?apikey=13173.30a420b0534ebad43ec1d24ba998a819.bcc3a14888c4568574c78165ac62d488

"msg":"请求成功",
"data":{
"id":9787517097471,
"name":"深入浅出OpenHarmony——架构、内核、驱动及应用开发全栈",
"subname":"",
"author":"李传钊",
"translator":null,
"publishing":"中国水利水电出版社",
"published":"2021",
"designed":"",
"code":"9787517097471",
"douban":35581417,
"doubanScore":0,
"numScore":1,
"brand":null,
"weight":null,
"size":null,
"pages":"304",
"photoUrl":"https://www.bookcovers.cn/index.php?client=szlib&isbn=9787517097471/cover",
"localPhotoUrl":"",
"price":"CNY88.00",
"froms":"douban_api2",
"num":0,
"createTime":"2022-07-28T13:55:58",
"uptime":"2022-07-28T13:55:58",
"authorIntro":"",
"description":"本书是由华为资深高级软件工程师、首位HarmonyOS&nbsp;HDE（Huawei Developer Expert）李传钊老师亲自编写的OpenHarmony操作系统技术图书。  本书从OpenHarmony操作系统的设计目标与设计思路开始，由表及里、深入浅出地讲解了OpenHarmony操作系统的架构、内核、驱动及应用开发基础与实战的全栈技术内容。在实战部分，从开发环境的安装配置开始，分别讲解了南向开发、北向开发的基本过程及实战案例。本书的英文版已在翻译之中，华为已将其作为面向全球推荐的OpenHarmony操作系统的官方技术教程之一。  本书可作为OpenHarmony操作系统开发人员、技术爱好者的自学或参考资料，也可作为高校相关专业的教材。",
"reviews":null,
"tags":null}
}
*/
/*
book类,内部信息由book库和library_level4_book库共同构成,其中id和position存储在library库中
 */
export class Book{
    /**
     * 馆内id,13位,前10位是isbn码的后10位,后3位是馆内编号
     */
    public id:string;
    /**
     * 中图法分类号
     */
    public sortId:string;
    /**
     * 书名
     */
    public name:string;
    /**
     * 子书名
     */
    public subname:string;
    /**
     * ISBN编码,13位
     */
    public isbn:string;
    /**
     * 作者
     */
    public author:string;
    /**
     * 译者
     */
    public translator:string;
    /**
     * 出版社
     */
    public publishing:string;
    /**
     * 出版年份
     */
    public published:string;
    /**
     * 页数
     */
    public pages:number;
    /**
     * 图片链接
     */
    public photoUrl:string;
    /**
     * 本地图片链接
     */
    public localPhotoUrl:string;
    /**
     * 作者信息
     */
    public authorInfo:string;
    /**
     * 书籍简介
     */
    public description:string;
    /**
     * 书籍目录
     */
    public content:string;
    /**
     * 书籍位置,所属场馆和具体位置之间用'|'分隔
     */
    public position:string;
    /**
     * 构造器
     */
    /* 构造示例
    new Book('0000000000000','TP314.86/65(1)','书名1',
            '子书名1','0000000000000','作者1','译者1','出版社1','2000',100),
     */
    constructor(
        id:string = '0000000000000',sortId:string = 'null',
        name:string = '书名',subname:string = '子书名',
        isbn:string = 'null',author:string = '作者',
        translator:string = '译者',
        publishing:string = '出版社',published:string = '2000',
        pages:number = 0, photoUrl:string = 'null',
        localPhotoUrl:string = 'null',authorInfo:string = '暂无',
        description:string = '暂无',content:string = '暂无',
        position:string = '||',
    ){
        this.id = id;
        this.sortId = sortId;
        this.name = name;
        this.subname = subname;
        this.isbn = isbn;
        this.author = author;
        this.translator = translator;
        this.publishing = publishing;
        this.published = published;
        this.pages = pages;
        this.photoUrl = photoUrl;
        this.localPhotoUrl = localPhotoUrl;
        this.authorInfo = authorInfo;
        this.description = description;
        this.content = content;
        this.position = position;
    }
    /**
     * 添加目录
     * @param content
     */
    addContent(content:string){
        this.content = content;
    }
    /**
     * 读取目录
     */
    getContent(){
        return this.content;
    }
    /**
     * 读取信息
     */
    getInfo(){
        return {
            id:this.id,
            sortId:this.sortId,
            name:this.name,
            isbn:this.isbn,
            description:this.description,
            position:this.position,
        };
    }
    /**
     * 根据id查询library和book数据库记录并更新该对象实例
     */
    searchById(){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('bookId',this.id);
        DA_HELPER.query(
            URI.Library_level4_book,
            ["bookId","inBookshelfBlock","positionRank","status","bookSortId"],
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`${TAG}::query err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'数据库查询出错'});
                    return;
                }
                if(resultSet.rowCount){
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    resultSet.goToFirstRow();
                    this.position = resultSet.getString(1) + '/' + resultSet.getString(2) + '/' + resultSet.getString(3);
                    this.sortId = resultSet.getString(4);
                    resultSet.close();
                    this.searchBySortId();
                    prompt.showToast({message:'数据查询完成'});
                }
                else{
                    resultSet.close();
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    prompt.showToast({message:'数据库无记录'});
                }
            }
        )
    }
    /**
     * 根据sortId查询book数据库记录并更新该对象实例,无id和position信息
     */
    searchBySortId(){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        DA_HELPER.query(
            URI.Book_URI,
            ["sortId","name","subname","isbn","author","translator","publishing","published","pages","photoUrl","localPhotoUrl","authorInfo","description","content"],
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`search book(sortId:${this.sortId}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库查询出错'});
                    return;
                }
                console.info(`search book(sortId:${this.sortId}) successful,rowCount:${resultSet.rowCount}`);
                if(resultSet.rowCount == 1){
                    resultSet.goToFirstRow();
                    this.name = resultSet.getString(1);
                    this.subname = resultSet.getString(2);
                    this.isbn = resultSet.getString(3);
                    this.author = resultSet.getString(4);
                    this.translator = resultSet.getString(5);
                    this.publishing = resultSet.getString(6);
                    this.published = resultSet.getString(7);
                    this.pages = resultSet.getDouble(8);
                    this.photoUrl = resultSet.getString(9);
                    this.localPhotoUrl = resultSet.getString(10);
                    this.authorInfo = resultSet.getString(11);
                    this.description = resultSet.getString(12);
                    this.content = resultSet.getString(13);
                    resultSet.close();
                    prompt.showToast({message:'book更新完成'});
                }
                else{
                    resultSet.close();
                    console.info(`search book(sortId:${this.sortId}) err : rowCount:${resultSet.rowCount}`);
                    prompt.showToast({message:'查询结果异常'});
                }
            }
        )
    }
    /**
     * 根据isbn查询book数据库记录并更新该对象实例,无id和position信息
     */
    searchByISBN(){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('isbn',this.isbn);
        DA_HELPER.query(
            URI.Book_URI,
            ["sortId","name","subname","isbn","author","translator","publishing","published","pages","photoUrl","localPhotoUrl","authorInfo","description","content"],
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`search book(isbn:${this.isbn}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库查询出错'});
                    return;
                }
                console.info(`search book(isbn:${this.isbn}) successful,rowCount:${resultSet.rowCount}`);
                if(resultSet.rowCount == 1){
                    resultSet.goToFirstRow();
                    this.sortId = resultSet.getString(0);
                    this.name = resultSet.getString(1);
                    this.subname = resultSet.getString(2);
//                    this.isbn = resultSet.getString(3);
                    this.author = resultSet.getString(4);
                    this.translator = resultSet.getString(5);
                    this.publishing = resultSet.getString(6);
                    this.published = resultSet.getString(7);
                    this.pages = resultSet.getDouble(8);
                    this.photoUrl = resultSet.getString(9);
                    this.localPhotoUrl = resultSet.getString(10);
                    this.authorInfo = resultSet.getString(11);
                    this.description = resultSet.getString(12);
                    this.content = resultSet.getString(13);
                    resultSet.close();
                    prompt.showToast({message:'查询完成'});
                }
                else{
                    resultSet.close();
                    console.info(`search book(isbn:${this.isbn}) err : rowCount:${resultSet.rowCount}`);
                    prompt.showToast({message:'查询结果异常'});
                }
            }
        )
    }
    /**
     * 重设书名
     * @param name
     */
    reset_name(name:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "name":name,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(name:${name}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.name = name;
                console.info(`update book(name:${name}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_isbn(isbn:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "isbn":isbn,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(isbn:${isbn}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.isbn = isbn;
                console.info(`update book(isbn:${isbn}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_sortId(sortId:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "sortId":sortId,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(sortId:${sortId}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.sortId = sortId;
                console.info(`update book(sortId:${sortId}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_subname(subname:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "subname":subname,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(subname:${subname}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.subname = subname;
                console.info(`update book(subname:${subname}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_author(author:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "author":author,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(author:${author}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.author = author;
                console.info(`update book(author:${author}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_translator(translator:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "translator":translator,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(translator:${translator}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.translator = translator;
                console.info(`update book(translator:${translator}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_publishing(publishing:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "publishing":publishing,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(publishing:${publishing}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.publishing = publishing;
                console.info(`update book(publishing:${publishing}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_published(published:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "published":published,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(published:${published}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.published = published;
                console.info(`update book(published:${published}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_pages(pages:number){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "pages":pages,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(pages:${pages}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.pages = pages;
                console.info(`update book(pages:${pages}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_photoUrl(photoUrl:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "photoUrl":photoUrl,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(photoUrl:${photoUrl}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.photoUrl = photoUrl;
                console.info(`update book(photoUrl:${photoUrl}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_localPhotoUrl(localPhotoUrl:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "localPhotoUrl":localPhotoUrl,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(localPhotoUrl:${localPhotoUrl}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.localPhotoUrl = localPhotoUrl;
                console.info(`update book(localPhotoUrl:${localPhotoUrl}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_authorInfo(authorInfo:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "authorInfo":authorInfo,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(authorInfo:${authorInfo}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.authorInfo = authorInfo;
                console.info(`update book(authorInfo:${authorInfo}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_description(description:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "description":description,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(description:${description}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.description = description;
                console.info(`update book(description:${description}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_content(content:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('sortId',this.sortId);
        let valuesBucket = {
            "content":content,
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(content:${content}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'book数据库更新出错'});
                    return;
                }
                this.content = content;
                console.info(`update book(content:${content}) successful,data:${data}`);
                prompt.showToast({message:'book数据库更新成功'});
            }
        )
    }
    reset_position(position:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('id',this.id);
        let positionlist = position.split('/');
        let valuesBucket = {
            "inBookshelfBlock":positionlist[0],
            "positionRank":positionlist[1],
            "status":positionlist[2],
        }
        DA_HELPER.update(
            URI.Book_URI,
            valuesBucket,
            predicates,
            (err,data)=>{
                if(err.code){
                    console.info(`update book(position:${position}) err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'library数据库更新出错'});
                    return;
                }
                this.position = position;
                console.info(`update book(position:${position}) successful,data:${data}`);
                prompt.showToast({message:'library数据库更新成功'});
            }
        )
    }
    lendToReader(){
        if(this.id.length != 13){
            prompt.showToast({message:'书籍借阅失败:书籍id格式错误'});
            return;
        }
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('bookId',this.id);
        DA_HELPER.query(
            URI.Library_level4_book,
            ["bookId","status"],
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`${TAG}::query err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'数据库查询出错'});
                    return;
                }
                if(resultSet.rowCount){
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    resultSet.goToFirstRow();
                    let status = resultSet.getString(1);
                    resultSet.close();
                    if(status == "在馆"){
                        let valuesBucket = {
                            "status":'借出',
                        };
                        DA_HELPER.update(
                            URI.Library_level4_book,
                            valuesBucket,
                            predicates,
                            (err,data)=>{
                                if(err.code){
                                    console.info(`update book(status) err:${JSON.stringify(err)}`);
                                    prompt.showToast({message:'书籍借阅失败:library数据库更新出错'});
                                    return;
                                }
                                console.info(`update book(status) successful,data:${data}`);
                                prompt.showToast({message:'书籍借阅成功'});
                            }
                        )
                    }
                    else{
                        prompt.showToast({message:'书籍借阅失败:书籍已借出'});
                    }
                }
                else{
                    resultSet.close();
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    prompt.showToast({message:'书籍借阅失败:数据库无记录'});
                }
            }
        )
    }
    returnFromReader(){
        if(this.id.length != 13){
            prompt.showToast({message:'书籍归还失败:书籍id格式错误'});
            return;
        }
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('bookId',this.id);
        DA_HELPER.query(
            URI.Library_level4_book,
            ["bookId","status"],
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`${TAG}::query err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'数据库查询出错'});
                    return;
                }
                if(resultSet.rowCount){
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    resultSet.goToFirstRow();
                    let status = resultSet.getString(1);
                    resultSet.close();
                    if(status == "借出"){
                        let valuesBucket = {
                            "status":'在馆',
                        };
                        DA_HELPER.update(
                            URI.Library_level4_book,
                            valuesBucket,
                            predicates,
                            (err,data)=>{
                                if(err.code){
                                    console.info(`update book(status) err:${JSON.stringify(err)}`);
                                    prompt.showToast({message:'书籍归还失败:library数据库更新出错'});
                                    return;
                                }
                                console.info(`update book(status) successful,data:${data}`);
                                prompt.showToast({message:'书籍归还成功'});
                            }
                        )
                    }
                    else{
                        prompt.showToast({message:'书籍归还失败:书籍已在馆'});
                    }
                }
                else{
                    resultSet.close();
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    prompt.showToast({message:'书籍归还失败:数据库无记录'});
                }
            }
        )
    }
    /**
     * 向book数据库插入书籍信息,不包含id和position
     */
    insertToBookRdb(){
        if(this.pages == NaN){//补丁:数据库插入失败可能原因
            this.pages = 0;
        }
        if(this.pages == null){//补丁:数据库插入失败可能原因
            this.pages = 0;
        }
        let valuesBucket = {
            "sortId":this.sortId,
            "name":this.name,
            "subname":this.subname,
            "isbn":this.isbn,
            "author":this.author,
            "translator":this.translator,
            "publishing":this.publishing,
            "published":this.published,
            "pages":this.pages,
            "photoUrl":this.photoUrl,
            "localPhotoUrl":this.localPhotoUrl,
            "authorInfo":this.authorInfo,
            "description":this.description,
            "content":this.content,
            };
        DA_HELPER.insert(
            URI.Book_URI,
            valuesBucket,
            (err,data)=>{
                if(err.code){
                    console.info("insertToBookRdb insert failed,err:"+JSON.stringify(err));
                    prompt.showToast({message:"操作失败:数据库插入出错"});
                    return;
                }
                if(data == 0){
                    console.info("insertToBookRdb insert failed,data:"+ data);
                    prompt.showToast({message:"操作失败:数据库插入失败"});
                    return;
                }
                console.info("insertToBookRdb insert successful, data:" + data);
            }
        )
    }
    /**
     * 向library数据库插入书籍信息,仅包含id、sortId和position,其中position分为三部分
     */
    insertToLibraryRdb(){
        let positionList = this.position.split('|');
        let valuesBucket = {
            "bookId":this.id,
            "bookSortId":this.sortId,
            "inBookshelfBlock":positionList[0],
            "positionRank":positionList[1],
            "status":positionList[2],
        };
        DA_HELPER.insert(
            URI.Library_level4_book,
            valuesBucket,
            (err,data)=>{
                if(err.code){
                    console.info("insertToLibraryRdb insert failed,err:"+JSON.stringify(err));
                    prompt.showToast({message:"操作失败:数据库插入出错"});
                    return;
                }
                if(data == 0){
                    console.info("insertToLibraryRdb insert failed,data:" + data);
                    prompt.showToast({message:"操作失败:数据库插入失败"});
                    return;
                }
                console.info("insertToLibraryRdb insert successful, data:" + data);
                prompt.showToast({message:"操作成功"});
            }
        )
    }
    /**
     * 删除library数据库中的指定书籍信息
     */
    deleteToLibraryRdb(){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo("bookId",this.id);
        DA_HELPER.query(
            URI.Library_level4_book,
            ["bookId","status"],
            predicates,
            (err,resultSet)=>{
                if(err.code){
                    console.info(`${TAG}::query err:${JSON.stringify(err)}`);
                    prompt.showToast({message:'数据库查询出错'});
                    return;
                }
                if(resultSet.rowCount){
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    resultSet.close();
                    DA_HELPER.delete(
                        URI.Library_level4_book,
                        predicates,
                        (err,data)=>{
                            if (err.code){
                                console.info("deleteLibraryBook delete failed, err: " + JSON.stringify(err));
                                prompt.showToast({
                                    message:"操作失败:数据库删除操作出错",
                                });
                                return;
                            }
                            console.info("deleteLibraryBook delete successful, data: " + data);
                            prompt.showToast({message:'操作成功'});
                        }
                    )
                }
                else{
                    resultSet.close();
                    console.info(`${TAG}::query successful,rowCount:${resultSet.rowCount}`);
                    prompt.showToast({message:'删除失败:数据库无记录'});
                }
            }
        )
    }
}

export class simpleBook{
    public bookName:string;
    public id:string;
    public backTime:string;
}

export function getBookItem(point:any):void{
    let lend:string;
    let id:string = point.user.id;
    let list:simpleBook[];
    let predicate = new dataAbility.DataAbilityPredicates();
    predicate.equalTo('id',id);
    DA_HELPER.query(
        URI.User_reader_URI,
        ['id','lend'],
        predicate,
        (err,resultSet)=>{
            if(err.code){
                console.info("getBookItem query err:"+JSON.stringify(err));
                return;
            }
            if(resultSet.rowCount){
                console.info("getBookItem query successful,result:"+resultSet.rowCount);
                resultSet.goToFirstRow();
                lend = resultSet.getString(resultSet.getColumnIndex('lend'));
                resultSet.close();
            }
            else{
                console.info("getBookItem query successful,but result:"+resultSet.rowCount);
                console.info("getBookItem err: not found lend");
                return;
            }
            //解析lend
            let lendNum:number = Number(lend.match(/{\d+?}/));
            let lendStr = lend.split('||')//分隔符
            for(let i = 1;i < lendNum;i++){
                list.push(JSON.parse(lendStr[i]));
            }
            //返回值传入point.list_book
            point.list_book = list;
            console.info("getBookItem successful");
            prompt.showToast({message:'借阅列表更新成功'});
        }
    )

//    return [
//        {bookName:'bookname1',id:'0000000000001',backTime:'2022/9/12'},
//        {bookName:'bookname2',id:'0000000000002',backTime:'2022/9/12'},
//        {bookName:'bookname3',id:'0000000000003',backTime:'2022/9/12'},
//        {bookName:'bookname4',id:'0000000000004',backTime:'2022/9/12'}
//    ];
}